﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using TAC;
using Arena;

namespace MenuTeste
{
    class Arena
    {
        private Vector2 cellSize = new Vector2(0.4f,0.4f);
        private GraphicsDevice device;
        public Matrix worldMatrix = Matrix.Identity;
        public MapaReal mapa = new MapaReal(2,35);
        Model myModel;
        const float SCALE = 0.0038f;
        Vector3 PositionZero = new Vector3(-1.7f, -0.25f, -3.0f);
        Texture2D textTexture, backTexture;
        //Text and background textures.
        SpriteFont spriteFont;

        /********Implementacao do padrao Singleton.
         * O acesso é feito chamando-se o metodo GetInstance(GraphicsDevice), 
         * que chama o construtor apenas quando a instancia é nula, caso contrario,
         * retorna a instancia (singleton).
        *********/

        //Instancia unica.
        private static Arena singleton;

        //Acesso publico a instancia.
        public static Arena GetInstance(GraphicsDevice graphicsDevice)
        {
            if (singleton == null)
            {
                singleton = new Arena(graphicsDevice);  // lazy
            }
            return singleton;
        }

        //Construtor privado
        private Arena(GraphicsDevice graphicsDevice)
        {
            device = graphicsDevice;
        }

        public void LoadContent(ref Model mModel, ContentManager content)
        {
            backTexture = content.Load<Texture2D>("background");
            textTexture = content.Load<Texture2D>("texttexture");
            spriteFont = content.Load<SpriteFont>("SpriteFont1");

            myModel = mModel;
            // Calculate the effect aspect ratio, projection, and view matrix
            float aspectRatio = (float)device.Viewport.Width / device.Viewport.Height;

            foreach(ModelMesh mesh in myModel.Meshes)
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                   MathHelper.ToRadians(45.0f),
                   aspectRatio, 1.0f, 20.0f);
                    effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 2.0f, 3.5f),
                    Vector3.Zero, Vector3.Up);
                    effect.EnableDefaultLighting();
                }
        }

        public void UnloadContent()
        {
            backTexture.Dispose();
            textTexture.Dispose();
        }

        public void Draw(ref SpriteBatch sBatch)
        {
            sBatch.Begin();
            sBatch.Draw(backTexture, new Rectangle(0, 0, device.DisplayMode.Width, device.DisplayMode.Height), Color.Aquamarine);
            sBatch.Draw(textTexture, new Rectangle(450, 660, 600, 200), Color.AliceBlue);
            sBatch.DrawString(spriteFont, "Tactical Advanced Combat.\n1: (" + mapa.personagens[0].getX() + 
                "," + mapa.personagens[0].getY() + ") damage:" + mapa.personagens[0].getAttribute(ID.DAMAGE) + 
                "\n2: (" + mapa.personagens[1].getX() + "," + mapa.personagens[1].getY() + ") damage:" + 
                mapa.personagens[1].getAttribute(ID.DAMAGE), new Vector2(510, 690), Color.Black);
            sBatch.End();

            Vector3 increment = Vector3.Zero;

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    increment.X = j * cellSize.X;
                    increment.Z = i * cellSize.Y;

                    foreach (ModelMesh mesh in myModel.Meshes)
                    {
                        foreach (BasicEffect effect in mesh.Effects) 
                        {
                            effect.World = Matrix.CreateScale(SCALE)*
                                Matrix.CreateRotationX(MathHelper.ToRadians(-90.0f))
                                *Matrix.CreateTranslation(PositionZero
                                + increment);
                        }

                        // Draw the current mesh
                        if(mapa.pontoCorreto(i,j)) mesh.Draw();

                    }
                }
            }
        }
    }
}
